﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;
using MvcXRepair.ViewModel;

namespace MvcXRepairCustomer.Repository.SQL
{
    public class UserDAO : BaseDAO
    {
        //public IEnumerable<Brand> GetAllActiveBrands()
        //{
        //    string spName = "GetAllActiveBrands";

        //    DataTable resultTab = null;
        //    List<Brand> brandList = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;
        //        resultTab = command.ExecuteDataTable();
        //    }
        //    if (resultTab != null && resultTab.Rows.Count > 0)
        //    {
        //        brandList = this.ConvertDataTableToBrandList(resultTab);
        //    }

        //    return brandList;

        //}

        //public bool DeactivateBrandByID(int brandId)
        //{
        //    string spName = "DeactivateBrandByID";

        //    object Rowcount = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;

        //        command.AddInParameter("ID", brandId);
        //        command.AddOutParameter("Rowcount", -1);
        //        command.ExecuteNonQuery();

        //        Rowcount = command.GetParameterValue("Rowcount");
        //    }

        //    long RowcountValue;

        //    if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
        //    {
        //        return true;
        //    }

        //    return false;
        //}

        public bool ActivateUserAccount(string actionvationCode)
        {
            string spName = "ActivateUserAccount";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ActivationCode", actionvationCode);

                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public User GetUserInfoByCredentials(string email, string password)
        {
            string spName = "GetUserInfoByCredentials";

            DataTable resultTab = null;
            User user = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("EmailAddress", email);
                command.AddInParameter("Password", password);

                resultTab = command.ExecuteDataTable();
            }

            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                user = this.ConvertDataTableToUserModel(resultTab);
            }

            return user;
        }

        public bool AddNewUser(User model)
        {
            string spName = "AddNewUser";
            object newIdentity = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("RoleID", model.RoleID);
                command.AddInParameter("Username", model.Username);
                command.AddInParameter("Password", model.Password);
                command.AddInParameter("EmailAddress", model.EmailAddress);
                command.AddInParameter("CreatedAt", model.CreatedAt);
                command.AddInParameter("CreatedBy", model.CreatedBy);
                command.AddInParameter("ActivationCode", model.ActivationCode); 

                command.AddOutParameter("NewIdentity", -1);
                command.ExecuteNonQuery();

                newIdentity = command.GetParameterValue("NewIdentity");
            }

            long newIdentityValue;

            if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool IsUsernameOccupied(string username)
        {
            string spName = "CheckUsernameExisting";
            object isExisting = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("Username", username);
                command.AddOutParameter("IsExisting", 1);
                command.ExecuteNonQuery();

                isExisting = command.GetParameterValue("IsExisting");
            }

            return isExisting.ToString() == "1";
        }

        public bool IsEmailOccupied(string email)
        {
            string spName = "CheckEmailExisting";
            object isExisting = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("Email", email);
                command.AddOutParameter("IsExisting", 1);
                command.ExecuteNonQuery();

                isExisting = command.GetParameterValue("IsExisting");
            }

            return isExisting.ToString() == "1";
        }

        public bool UpdateUserPasswordByEmailAddress(string email, string newPassword)
        {
            string spName = "UpdateUserPassword";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("EmailAddress", email);
                command.AddInParameter("NewPassword", newPassword);

                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        private User ConvertDataTableToUserModel(DataTable data)
        {
            User user = null;

            if (data != null && data.Rows.Count == 1)
            {
                user = new User();
                DataRow infoRow = data.Rows[0];

                user.ID = SqlUtil<int>.GetValue(infoRow, "ID");
                user.Password = SqlUtil<string>.GetValue(infoRow, "Password");
                user.RoleID = SqlUtil<int>.GetValue(infoRow, "RoleID");
                user.Salt = SqlUtil<string>.GetValue(infoRow, "Salt");
                user.Username = SqlUtil<string>.GetValue(infoRow, "Username");
                user.Alias = SqlUtil<string>.GetValue(infoRow, "Alias");
                user.EmailAddress = SqlUtil<string>.GetValue(infoRow, "EmailAddress");
            }

            return user;
        }

        //private List<Brand> ConvertDataTableToBrandList(DataTable data)
        //{
        //    List<Brand> brands = null;

        //    if (data != null)
        //    {
        //        brands = new List<Brand>();

        //        foreach (DataRow dr in data.Rows)
        //        {
        //            var serviceModel = new Brand();

        //            serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
        //            serviceModel.GenericTypesID = (DeviceTypes)SqlUtil<int>.GetValue(dr, "GenericTypesID");
        //            serviceModel.BrandName = SqlUtil<string>.GetValue(dr, "BrandName");
        //            serviceModel.IsActive = SqlUtil<bool>.GetValue(dr, "IsActive");
        //            serviceModel.CreatedAt = SqlUtil<DateTime>.GetValue(dr, "CreatedAt");
        //            serviceModel.CreatedBy = SqlUtil<String>.GetValue(dr, "CreatedBy");
        //            serviceModel.LastUpdatedAt = SqlUtil<DateTime>.GetValue(dr, "LastUpdatedAt");
        //            serviceModel.LastUpdatedBy = SqlUtil<String>.GetValue(dr, "LastUpdatedBy");

        //            brands.Add(serviceModel);
        //        }
        //    }

        //    return brands;
        //}
    }
}
